/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.posplus.losspreventiondataimporter;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.log4j.Logger;

/**
 *
 * @author ScottW
 */
public class Timespan {
    private long milliseconds;
    private long seconds;
    private long minutes;
    private long hours;
    
    private int scale = 2;
    
    private static final long millisInSeconds = 1000L;
    
    
    public Timespan(long milliseconds){
        recalculate(milliseconds);
    }
    
    private void recalculate(long totalMilliseconds){ 
        
        milliseconds = totalMilliseconds % millisInSeconds;        
        
        long totalSeconds = (totalMilliseconds - milliseconds) / 1000;
        
        seconds = totalSeconds % 60;
        
        long totalMinutes = (totalSeconds - seconds) / 60;
        
        minutes = totalMinutes % 60;
        
        hours = (totalMinutes - minutes) / 60;
    }

    public void setMilliseconds(long milliseconds) {
        recalculate(milliseconds);
    }

    public long getHours() {
        return hours;
    }

    public long getMilliseconds() {
        return milliseconds;
    }

    public long getMinutes() {
        return minutes;
    }

    public long getSeconds() {
        return seconds;
    }

    
    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        
        if(hours > 0)
            builder.append(hours).append(" h");
        
        if(minutes > 0){
            if(builder.length() > 0) builder.append(" ");
            builder.append(minutes).append(" m");
        }
        
        if(seconds > 0){
            if(builder.length() > 0) builder.append(" ");
            builder.append(seconds).append(" s");
        }
        
        if(milliseconds > 0){
            if(builder.length() > 0) builder.append(" ");
            builder.append(milliseconds).append(" ms");
        }            
        
        return builder.toString();
    }
    
    Logger logger = Logger.getLogger(this.getClass());
}
